<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

class Action_Node_TopMenu {
  private $rh;
  private $max_items = 5;
  
  public function __construct( $rh ) {
    $this->rh = $rh;
  }
  
  public function getMenuHtml() {
    $view = $this->rh->views->findByName('top_menu_items');
    
    $node = $this->rh->ctx->store->getObject(1);
    
    if ( isset($node->data['top_menu']) ) {
      $view->data['top_menu_items'] = $node->data['top_menu'];
    }
    
    return $view->getContent();
  }

  public function getMenuSettingsHtml() {
    $view = $this->rh->views->findByName('top_menu_settings');
    
    $node = $this->rh->ctx->store->getObject(1);
    
    if ( isset($node->data['top_menu']) ) {
      $view->data['top_menu_items'] = $node->data['top_menu'];
    }
    
    $count = 1;
    foreach ($view->data['top_menu_items'] as $key=>$val) {
      $view->data['top_menu_items'][$key]['count'] = $count;
      $count++;
    }
    
    if ( count($view->data['top_menu_items']) < $this->max_items ) {
      for ( $i=count($view->data['top_menu_items'])+1; $i<=$this->max_items; $i++ ) {
        $view->data['top_menu_items'][]['count'] = $i;
      }
    }
    
    return $view->getContent();
  }
}